package com.yj.timecapsule.common.context;

import com.yj.timecapsule.common.enums.ErrorCode;
import com.yj.timecapsule.common.error.BusinessException;
import com.yj.timecapsule.entity.user.vo.UserLoginVo;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Sean
 * @CreateTime 2024-07-31  20:19
 * @Description
 * @Version 1.0
 */
@Slf4j
public class LoginUserContext {
    private static ThreadLocal<UserLoginVo> USER_VO = new ThreadLocal<>();
    public static UserLoginVo getUser() {
        return LoginUserContext.USER_VO.get();
    }
    public static void setUser(UserLoginVo member) {
        LoginUserContext.USER_VO.set(member);
    }

    public static Long getId(){
        if (getUser() != null){
            Long id = getUser().getId();
            log.info("当前登录会员: {}",id);
            return id;
        }else {
            throw new BusinessException(ErrorCode.USER_NOT_LOGIN);
        }
    }

    public static void removeUser() {
        LoginUserContext.USER_VO.remove();
    }
}